let LineToStops = require('../busModels/LineToStops');
let moment = require('moment');
let sizeof = require('./sizeof.js');


var Segment = require('segment');
		// 创建实例
var segment = new Segment();
		// 使用默认的识别模块及字典，载入字典文件需要1秒，仅初始化时执行一次即可
segment.useDefault();

let lineIndex = 0;
let stopIndex = 0;
let lineList = {};
let stopList = {};
let antiLineList = {};
let antiStopList = {};
let linesTable = [];
let stopsTable = [];
let choices={'key':[{
		bus:[],
		time:0,
}]};
choices = {};
let lineToLine = {};
let resultLength = 7;
let startTime = moment();
let stopToStop = [];
// var Promise = global.Promise;
let query ;
LineToStops.find({'city':'shanghai'}).exec(function(err, lines){
	
	for(let i=0;i<lines.length;i++){
		let line = lines[i];
		let stops = line.stops;
		let lineName = line.line;
		lineList[lineName] = lineIndex++;
		for(let j=0;j<stops.length;j++){

			let stop = stops[j];
			
			if(stopList[stop]==null||stopList[stop]==undefined){
				stopList[stop] = stopIndex++;
			}
		}

	}
	for(let prop in lineList){
		antiLineList[lineList[prop]]=prop;
	}
	for(let prop in stopList){
		antiStopList[stopList[prop]]=prop;
	}
	stopsTable = new Array(stopList.length);
	for(let i=0;i<lines.length;i++){
		let line = lines[i];
		let lineName = line.line;
		let stops = line.stops;

		let stopsTemp = [];
		for(let j=0;j<stops.length;j++){
			let stopNum = stopList[stops[j]];
			
			stopsTemp.push(stopNum);
			if(stopsTable[stopNum] == undefined){
				stopsTable[stopNum] = [{
					lineName:lineList[lineName],
					stopIndex:j
				}];
			}else{
				stopsTable[stopNum].push({
					lineName:lineList[lineName],
					stopIndex:j
				});
			}
			// stopsTable[stops]
		}
		linesTable.push(stopsTemp);

	}
	

	for(let i=0;i<stopsTable.length;i++){
		let lines = stopsTable[i];
		for(let j=0;j<lines.length;j++){
			let left = lines[j];
			for(let k=0;k<lines.length;k++){
				let right = lines[k];
				if(j==k)continue;
				let key = left.lineName+'--'+right.lineName;
				if(lineToLine[key] == undefined){
					lineToLine[key] = [{
						leftStopIndex:left.stopIndex,
						rightStopIndex:right.stopIndex,
						jiaodianNum:i,
					}];
				}else{
					lineToLine[key].push({
						leftStopIndex:left.stopIndex,
						rightStopIndex:right.stopIndex,
						jiaodianNum:i,
					});
				}
			}
		}
	}

	
	
});
function getResult (startName,endName,select){
		let start = stopList[startName];
		let end = stopList[endName];
		choices={};
		zhida(start,end);
		if(choices['zhida'].length < 5 ){

			yicihuancheng(start,end);
			if(choices['zhida'].length + choices['yicihuancheng'].length  < 5){
				ercihuancheng(start,end);
			}
		}
		return choices;

	}
	query = getResult;
	
	function numAscSort(a,b){
	 	return a.time - b.time;
	}
	
	function zhida(start,end){
		let startLines = stopsTable[start];
		let endLines = stopsTable[end];
		choices['zhida']=[];
		for(let i=0;i<startLines.length;i++){
			for(let j=0;j<endLines.length;j++){
				let startLineName = startLines[i].lineName;
				let startStopIndex = startLines[i].stopIndex;
				let endLineName = endLines[j].lineName;
				let endStopIndex = endLines[j].stopIndex;
				if(startLineName != endLineName ) continue;
				if(startStopIndex > endStopIndex) continue;
				
				
				choices['zhida'].push({
					bus:[antiLineList[startLineName]],
					time:(endStopIndex-startStopIndex)*3,
					stopLength:[endStopIndex-startStopIndex],
					type:0,
					jiaodian:[],
					price:2
				});
			}
		}
		choices['zhida'] = choices['zhida'].sort(numAscSort).slice(0,resultLength);
	}
	function yicihuancheng(start,end){
		let startLines = stopsTable[start];
		let endLines = stopsTable[end];
		choices['yicihuancheng']=[];
		for(let i=0;i<startLines.length;i++){
			for(let j=0;j<endLines.length;j++){
				let startLineName = startLines[i].lineName;
				let startStopIndex = startLines[i].stopIndex;
				let endLineName = endLines[j].lineName;
				let endStopIndex = endLines[j].stopIndex;
				let temp = lineToLine[startLineName+'--'+endLineName];
				if(temp==undefined&&temp==null){
					continue;
				}
				for(let k=0;k<temp.length;k++){
					var leftStopIndex = temp[k].leftStopIndex;
					var rightStopIndex = temp[k].rightStopIndex;
					if(startStopIndex >= leftStopIndex) continue;
					if(endStopIndex <= rightStopIndex) continue;
					var jiaodian = antiStopList[temp[k].jiaodianNum];
					choices['yicihuancheng'].push({
						bus:[antiLineList[startLineName],antiLineList[endLineName]],
						time:(leftStopIndex-startStopIndex+endStopIndex-rightStopIndex)*3+5,
						stopLength:[leftStopIndex-startStopIndex,endStopIndex-rightStopIndex],
						type:1,
						jiaodian:[jiaodian],
						price:4
					});
				}
			}
		}
		choices['yicihuancheng'] = choices['yicihuancheng'].sort(numAscSort).slice(0,resultLength);
	}

	function ercihuancheng(start,end){
		choices['ercihuancheng']=[];
		let beginLines = stopsTable[start];
		let endLines = stopsTable[end];
		for(let z=0;z<beginLines.length;z++){
			let beginLineName = beginLines[z].lineName;
			let beginStopIndex = beginLines[z].stopIndex;
			let beginStops = linesTable[beginLineName];
			for(y=beginStopIndex+1;y<beginStops.length;y++){
				let startLines = stopsTable[beginStops[y]];
				for(let i=0;i<startLines.length;i++){
					for(let j=0;j<endLines.length;j++){
						let startLineName = startLines[i].lineName;
						let startStopIndex = startLines[i].stopIndex;
						let endLineName = endLines[j].lineName;
						let endStopIndex = endLines[j].stopIndex;
						let temp = lineToLine[startLineName+'--'+endLineName];
						if(temp==undefined&&temp==null){
							continue;
						}
						for(let k=0;k<temp.length;k++){
							var leftStopIndex = temp[k].leftStopIndex;
							var rightStopIndex = temp[k].rightStopIndex;
							if(startStopIndex >= leftStopIndex) continue;
							if(endStopIndex <= rightStopIndex) continue;
							var jiaodian = antiStopList[temp[k].jiaodianNum];
							choices['ercihuancheng'].push({
								bus:[antiLineList[beginLineName],antiLineList[startLineName],antiLineList[endLineName]],
								time:(y-beginStopIndex+leftStopIndex-startStopIndex+endStopIndex-rightStopIndex)*3+5,
								stopLength:[y-beginStopIndex,leftStopIndex-startStopIndex,endStopIndex-rightStopIndex],
								type:2,
								jiaodian:[antiStopList[beginStops[y]],jiaodian],
								price:6
							});
						}
					}
				}
			}
		}		
		choices['ercihuancheng'] = choices['ercihuancheng'].sort(numAscSort).slice(0,resultLength);
	}
	function like(text,list){
		var result = [];
		var words = [];
		var words = segment.doSegment(text, {
		  simple: true
		});
		// console.log(words);
		for(var i=0;i<text.length;i++){
			for(var j=i+1;j<=text.length;j++){
				
				words.push(text.slice(i,j));
			}
		}
		words.sort(function(a,b){
			return b.length - a.length;
		});
		// console.log(words);
		for(var i=0;i<words.length;i++){
			var word = words[i];
			for(var j in list){
				if(result.indexOf(j) != -1)continue;
				if(j.indexOf(word) != -1){
					result.push(j);
					if(result.length >= 5){
						return result;
					};
				}
			}
		}
		return result;
	}
module.exports = {
	transfer:query,
	line:function(name){
		var lineNum = lineList[name];
		var stopsNum = linesTable[lineNum];
		var stops = [];
		for(let i=0;i<stopsNum.length;i++){
			stops.push(antiStopList[stopsNum[i]]);
		}
		return stops;
	},
	stop:function(name){
		var stopNum = stopList[name];
		var linesNum = stopsTable[stopNum];
		// console.log(linesNum);
		var lines = [];
		for(let i=0;i<linesNum.length;i++){
			lines.push(antiLineList[linesNum[i].lineName]);
		}
		return lines;
	},
	lineLike:function(text){
		return like(text,lineList);
	},
	stopLike:function(text){
		return like(text,stopList);
	},

};
